﻿//  Copyright © Decebal Mihailescu 2013-2014
//  All rights reserved. This source code and binaries are distributed under 
//  Microsoft Public PatternLicense (Ms-PL)
//  The latest code is available at http://VSDesignPatterns.codeplex.com

//  Redistributions of source code must retain the above copyright notice, 
//  this list of conditions and the following disclaimer. 
//  Redistributions in binary form must reproduce the above copyright notice, 
//  this list of conditions and the following disclaimer in the documentation 
//  and/or other materials provided with the distribution. 

//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
//  PURPOSE. IT CAN BE DISTRIBUTED FREE OF CHARGE AS LONG AS THIS HEADER 
//  REMAINS UNCHANGED.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#if !VS2015
//using Extensibility;
#endif
using System.Collections;
using System.Windows.Forms;

namespace DesignPatterns
{
    using DesignPatternsWizard;
    using DesignPattterns;
    using EnvDTE;
    using EnvDTE80;
    //using VSLangProj;//for references
    //using VSLangProj2;
    using Common;

    internal class ProjectInfoDTE : ProjectInfo, IServiceProvider
    {
        Project _projectDTE;
        internal ProjectInfoDTE(Project pr)
            : base(pr.GuessProjectType(), pr.DTE.Application.Solution.FullName)
        {
            _projectDTE = pr;
            this.ProjectName = pr.Name;
            this.FileName = pr.FileName;

            this.ProjectProperties = pr.GetProjectProperties();
            //Property prtypes = pr.Properties.OfType<Property>().SingleOrDefault(p => p.Name == "ProjectType");
            //Properties props = prtypes.Collection;
            //var obj = props.OfType<Property>().FirstOrDefault();
            //var guid = props.OfType<System.Guid>().FirstOrDefault();
            if (Utils.VSVersion != pr.DTE.Application.Version)
                MessageBox.Show(pr.DTE.Application as IWin32Window, "The project version does not match with the add-in.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            if (CodeLangEnum.NativeCpp == this.CodeLang)
            {
                if (ProjectProperties != null && ProjectProperties["keyword"] == "ManagedCProj")
                    CodeLang = CodeLangEnum.ManagedCpp;
            }
            /*
                       //must add VSLangProj reference
                       VSProject vsProject = pr.Object as VSProject;
                       if (vsProject != null)
                       {
                           foreach (Reference reference in vsProject.References)
                           {
                               // Do cool stuff here
                           }
                       }

           
                       Property p = pr.Properties.Item("References");
                       Properties coll = p.Value as Properties;
                       Property[] nestedColls = coll.GetNestedProperties().ToArray();
                       string[] namesFirstLetter = nestedColls.Select(prop => prop.Name).ToArray();
                       Property[] flatColl = coll.GetFlatProperties().ToArray();
                       namesFirstLetter = flatColl.Select(prop => prop.Name).ToArray();
                       */
        }

        #region IServiceProvider Members

        override public object  GetService(Type serviceType)
        {
            if (serviceType == typeof(Project))
                return _projectDTE;
            if ( serviceType == typeof(ProjectInfoDTE))
                return this;
            return base.GetService(serviceType);
        }

        #endregion
    }
}
